package com.junt.library.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.junt.library.model.Media;

import java.util.Arrays;
import java.util.List;

public class PhotoUtils {
    /**
     * 请求拍照
     */
    public static final int REQUEST_CAPTURE = 1;

    private static Uri captureUri = Uri.EMPTY;


    /**
     * 调起相机拍照
     */
    public static Uri capture(Activity activity, int requestCode) {
        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 判断是否有相机
        if (captureIntent.resolveActivity(activity.getPackageManager()) != null) {
            captureUri = createImageUri(activity);
            if (captureUri != null) {
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);
                captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                activity.startActivityForResult(captureIntent, requestCode);
            }
        }
        return captureUri;
    }

    /**
     * 创建图片地址uri,用于保存拍照后的照片 Android 10以后使用这种方法
     */
    private static Uri createImageUri(Activity activity) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.SIZE, 100);
        contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis());
        return activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
    }

    public static Uri obtain(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CAPTURE && resultCode == Activity.RESULT_OK) {
            return captureUri;
        }
        return Uri.EMPTY;
    }


//    private void getImageInfo(Activity activity,) {
//        Cursor cursor = getContentResolver().query(captureUri, null, null, null, null);
//        List<String> columnNames = Arrays.asList(cursor.getColumnNames());
//        int idColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID);
//        int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
//        int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
//        int absolutePathColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
//        int dateModifiedColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED);
//        int mimeTypeColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE);
//        int widthColumn = -1;
//        if (columnNames.contains(MediaStore.Images.ImageColumns.WIDTH)) {
//            widthColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.WIDTH);
//        }
//        int heightColumn = -1;
//        if (columnNames.contains(MediaStore.Images.ImageColumns.HEIGHT)) {
//            heightColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.HEIGHT);
//        }
//        int durationColumn = -1;
//        if (columnNames.contains("duration")) {
//            durationColumn = cursor.getColumnIndexOrThrow("duration");
//        }
//        int orientationColumn = -1;
//        if (columnNames.contains("orientation")) {
//            orientationColumn = cursor.getColumnIndexOrThrow("orientation");
//        }
//        int relativePathColumn = -1;
//        if (columnNames.contains("relative_path")) {
//            relativePathColumn = cursor.getColumnIndexOrThrow("relative_path");
//        }
//
//        //数据库遍历
//        cursor.moveToNext();
//        long id = cursor.getLong(idColumn);
//        String name = cursor.getString(nameColumn);
//        long size = cursor.getLong(sizeColumn);
//        String mimeType = cursor.getString(mimeTypeColumn);
//        long dateModified = cursor.getLong(dateModifiedColumn);
//        String absolutePath = cursor.getString(absolutePathColumn);
//        long duration = 0;
//        if (durationColumn >= 0) {
//            duration = cursor.getInt(durationColumn);
//        }
//        int orientation = 0;
//        if (orientationColumn >= 0) {
//            orientation = cursor.getInt(orientationColumn);
//        }
//        int width = 0;
//        if (widthColumn >= 0) {
//            width = cursor.getInt(widthColumn);
//        }
//        int height = 0;
//        if (widthColumn >= 0) {
//            height = cursor.getInt(heightColumn);
//        }
//        String relativePath = "";
//        if (relativePathColumn >= 0) {
//            relativePath = cursor.getString(relativePathColumn);
//        }
//
//        Media media = new Media(captureUri, name, size,
//                duration, dateModified, mimeType, orientation,
//                width, height, relativePath, absolutePath);
//
//        cursor.close();
//
//        Log.i(TAG, "getImageInfo: " + media.toString());
//    }
}
